﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FortuneHeavenCommon.ComMethod;
using System.Xml.Serialization;
using System.IO;
using FortuneHeavenBAL;
using FortuneHeavenCommon.DataMap;

namespace FortuneHeaven.Web.Common
{
    public class AuthorityCenter
    {
        public static bool ValidateUser(string user, string password, string validecode, ref string errorinfor)
        {
            if (WebSession.Session[ValidationCode.Name] == null)
            {
                errorinfor = "验证码失效";
                return false;
            }

            if (((string)WebSession.Session[ValidationCode.Name]).ToLower() != validecode.ToLower())
            {
                errorinfor = "验证码错误";
                return false;
            }
            AccountsInfoBO bo = new AccountsInfoBO();
            AccountsInfo infor= bo.CheckUserPwd(user, CommonEncrypt.Encrypt(password.Trim()).ToLower());
            if (infor == null)
            {
                errorinfor = "帐号或者密码不正确";
                return false;
            }
            else
            {
                CurrentUserID = infor.UserID;
                CurrentUserName = infor.Accounts;
                CurrentGameID = infor.GameID;
                InitalAllNeedTable(infor.UserID);
            }
            return true;
        }

        public static AccountsInfo ValidateUserNoLogin(string user, string password, string validecode, ref string errorinfor)
        {
            if (WebSession.Session[ValidationCode.Name] == null)
            {
                errorinfor = "验证码失效";
                return null;
            }

            if (((string)WebSession.Session[ValidationCode.Name]).ToLower() != validecode.ToLower())
            {
                errorinfor = "验证码错误";
                return null;
            }
            AccountsInfoBO bo = new AccountsInfoBO();
            AccountsInfo infor = bo.CheckUserPwd(user, CommonEncrypt.Encrypt(password.Trim()).ToLower());
            if (infor == null)
            {
                errorinfor = "帐号或者密码不正确";
                return null;
            }
            return infor;
        }

        public static bool CheckValidateCode(string validecode)
        {
            if (WebSession.Session[ValidationCode.Name] == null)
            {
                return false;
            }

            if (((string)WebSession.Session[ValidationCode.Name]).ToLower() != validecode.ToLower())
            {
                return false;
            }
            return true;
        }

        /// <summary>
        /// 插入所有网站需要的数据.为了通过游戏注册的用户能顺利登陆网站
        /// </summary>
        /// <param name="userid"></param>
        public static void InitalAllNeedTable(int userid)
        {
            //UserFortuneBO fortunebo = new UserFortuneBO();
            //UserFortune fortune = fortunebo.GetUserFortune(userid);
            //if (fortune == null)
            //{
            //    fortune = new UserFortune();
            //    fortune.CurrentValue = 0;
            //    fortune.UpdateTime = DateTime.Now;
            //    fortune.UserID = userid;
            //    fortunebo.Add(fortune);
            //}
            UserInforBO userbo = new UserInforBO();
            UserInfor userinfor = userbo.GetUserInfor(userid);
            if (userinfor == null)
            {
                userinfor = new UserInfor();
                userinfor.ID = 0;
                userinfor.UserID = userid;
                userinfor.FortuneValue = 0;
                userbo.SaveUserInfor(userinfor);
            }
        }

        public static bool CheckLogin()
        {
            if (CurrentUserID < 0)
            {
                HttpContext.Current.Response.Clear();
                string script = string.Format("<script> document.location='{0}'; </script>",
                    VirtualPathUtility.ToAbsolute("~/Default.aspx"));
                HttpContext.Current.Response.Write(script);
                HttpContext.Current.Response.End();
                return false;
            }
            return true;
        }

        public static int CurrentUserID
        {
            get
            {
                object oj = WebSession.Session["UserID"];
                if (oj == null)
                {
                    return -1;
                }

                return (int)(oj);
            }
            set
            {
                WebSession.Session["UserID"] = value;
            }
        }

        public static string CurrentUserName
        {
            get
            {
                string name = (string)WebSession.Session["UserName"];

                return name;
            }
            set
            {
                WebSession.Session["UserName"] = value;
            }
        }

        public static int CurrentGameID
        {
            get
            {
                object oj = WebSession.Session["GameID"];
                if (oj == null)
                {
                    return -1;
                }

                return (int)(oj);
            }
            set
            {
                WebSession.Session["GameID"] = value;
            }
        }
        public static void Unlogin()
        {
            CurrentUserID = -1;
            CurrentGameID = -1;
            CurrentUserName = "";
        }
    }
}